'SmallStrong@yfbcsqw001'
import re

from captcha.helpers import captcha_image_url, captcha_audio_url
from captcha.models import CaptchaStore
from captcha.conf import settings as ca_settings
from django.contrib import admin
from django.urls import path, include
from django.contrib.staticfiles.views import serve
from django.urls import re_path

# ======================== #
# **  drf-yasg swagger  ** #
# ======================== #
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework.views import APIView

from Apps.common.response import SuccessResponse

schema_view = get_schema_view(
    openapi.Info(
        title="API文档",
        default_version='v1',
        description="接口文档明细，可在此调试后端接口",
        terms_of_service="http://www.baidu.com",
        contact=openapi.Contact(name="友情提示", url="http://www.baidu.com"),
        license=openapi.License(name="please click me", url="http://www.baidu.com"),
    ),
    public=True,
    authentication_classes=(),
    permission_classes=(permissions.AllowAny,),
)


def return_static(request, path, insecure=True, **kwargs):
    return serve(request, path, insecure, **kwargs)


class CaptchaRefresh(APIView):
    authentication_classes = []
    permission_classes = []

    def get(self, request, ):
        new_key = CaptchaStore.pick()
        to_json_response = {
            "key": new_key,
            "image_url": captcha_image_url(new_key),
            "audio_url": captcha_audio_url(new_key) if ca_settings.CAPTCHA_FLITE_PATH else None,
        }
        return SuccessResponse(to_json_response)


urlpatterns = [
    path('admin/', admin.site.urls),
    # 上传文件可访问
    re_path(r'^static/(?P<path>.*)$', return_static, name='static'),

    # drf-yasg swagger
    # https://drf-yasg.readthedocs.io/en/stable/readme.html
    re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    re_path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),

    re_path(r"captcha/refresh/$", CaptchaRefresh.as_view(), name="captcha-refresh"),  # 刷新验证码
    re_path('captcha/', include('captcha.urls')),  # 图片验证码 路由

    path('api/', include('Apps.urls')),
]

